package test;

import anaydis.sort.Sorter;
import sort.benchmark.Benchmark;
import sort.data.IntegerDataSetGenerator;
import sort.impl.ShellSorter;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SorterTestExcel {
    private WritableWorkbook workbook;
    private WritableSheet sheet;

    public void init() {
        try {
            workbook = Workbook.createWorkbook(new File("practica3.xls"));
            sheet = workbook.createSheet("First Sheet", 0);

            IntegerDataSetGenerator generator = new IntegerDataSetGenerator();
            Benchmark benchmark = new Benchmark();

            /*BubbleSorter bubbleSorter = new BubbleSorter();
            bubbleSorter.addSorterListener(benchmark);

            SelectionSorter selectionSorter = new SelectionSorter();
            selectionSorter.addSorterListener(benchmark);

            InsertionSorter insertionSorter = new InsertionSorter();
            insertionSorter.addSorterListener(benchmark);*/

            ShellSorter shellSorter = new ShellSorter();
            shellSorter.addSorterListener(benchmark);

            makeFirstColumn();
            makeSecondColumn();
            makeThirdColumn();

            shellSorter.setHList(initFirstSequence());
            sheet.addCell(new jxl.write.Label(3, 0, "SHELL SORT (1)"));
            makeComparisonColumns(3,generator, benchmark, shellSorter);

            shellSorter.setHList(initSecondSequence());
            sheet.addCell(new jxl.write.Label(4, 0, "SHELL SORT (2)"));
            makeComparisonColumns(4,generator, benchmark, shellSorter);

            /*sheet.addCell(new jxl.write.Label(3, 0, "BUBBLE SORT"));
            makeComparisonColumns(3, generator, benchmark, bubbleSorter);

            sheet.addCell(new jxl.write.Label(4, 0, "SELECTION SORT"));
            makeComparisonColumns(4, generator, benchmark, selectionSorter);

            sheet.addCell(new jxl.write.Label(5, 0, "INSERTION SORT"));
            */

            workbook.write();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

    private static List<Integer> initFirstSequence() {
        List<Integer> firstSequence = new ArrayList<Integer>();
        firstSequence.add(1657);
        firstSequence.add(4193);
        firstSequence.add(1073);
        firstSequence.add(281);
        firstSequence.add(77);
        firstSequence.add(23);
        firstSequence.add(8);
        return firstSequence;
    }

    private static List<Integer> initSecondSequence() {
        List<Integer> secondSequence = new ArrayList<Integer>();
        secondSequence.add(9841);
        secondSequence.add(3280);
        secondSequence.add(1093);
        secondSequence.add(364);
        secondSequence.add(121);
        secondSequence.add(40);
        secondSequence.add(13);
        secondSequence.add(4);
        return secondSequence;
    }

    private void makeFirstColumn() throws WriteException {
        sheet.addCell(new jxl.write.Label(0, 0, "N"));
        sheet.addCell(new jxl.write.Number(0, 1, 100));
        sheet.addCell(new jxl.write.Number(0, 10, 1000));
        sheet.addCell(new jxl.write.Number(0, 19, 10000));
        sheet.addCell(new jxl.write.Number(0, 28, 50000));
        /*sheet.addCell(new jxl.write.Number(0, 37, 500));
        sheet.addCell(new jxl.write.Number(0, 46, 1000));
        sheet.addCell(new jxl.write.Number(0, 55, 2000));
        sheet.addCell(new jxl.write.Number(0, 64, 3000));
        sheet.addCell(new jxl.write.Number(0, 73, 4000));
        sheet.addCell(new jxl.write.Number(0, 82, 5000));*/
    }

    private void makeSecondColumn() throws WriteException{
        sheet.addCell(new jxl.write.Label(1,0,"List type"));
        for(int i = 1; i <84;i+=9){
            sheet.addCell(new jxl.write.Label(1,i,"Ascending"));
            sheet.addCell(new jxl.write.Label(1,i+3,"Descending"));
            sheet.addCell(new jxl.write.Label(1,i+6,"Random"));
        }
    }

    private void makeThirdColumn() throws WriteException {
        for (int i = 1; i < 90; i += 3) {
            sheet.addCell(new jxl.write.Label(2, i, "Comparisons"));
            sheet.addCell(new jxl.write.Label(2, i + 1, "Swaps"));
            sheet.addCell(new jxl.write.Label(2, i + 2, "Time"));
        }
    }

    private void makeComparisonColumns(int sorterColumn, IntegerDataSetGenerator generator, Benchmark benchmark, Sorter sorter) throws WriteException {
        //makeSorterColumns(sorterColumn, generator, sorter, benchmark, 1, 10);
        //makeSorterColumns(sorterColumn, generator, sorter, benchmark, 10, 50);
        makeSorterColumns(sorterColumn, generator, sorter, benchmark, 1, 100);
        //makeSorterColumns(sorterColumn, generator, sorter, benchmark, 28, 250);
        //makeSorterColumns(sorterColumn, generator, sorter, benchmark, 37, 500);
        makeSorterColumns(sorterColumn, generator, sorter, benchmark, 10, 1000);
        //makeSorterColumns(sorterColumn, generator, sorter, benchmark, 55, 2000);
        //makeSorterColumns(sorterColumn, generator, sorter, benchmark, 64, 3000);
        //makeSorterColumns(sorterColumn, generator, sorter, benchmark, 73, 4000);
        //makeSorterColumns(sorterColumn, generator, sorter, benchmark, 82, 5000);
        makeSorterColumns(sorterColumn, generator, sorter, benchmark, 19, 10000);
        makeSorterColumns(sorterColumn, generator, sorter, benchmark, 26, 50000);
    }

    private void makeSorterColumns(int sorterColumn, IntegerDataSetGenerator generator, Sorter sorter, Benchmark benchmark, int i, int n) throws WriteException {
       /* List<Integer> ascendingList = generator.createAscending(n);
        long timeA1 = System.currentTimeMillis();
        sorter.sort(generator.getComparator(), ascendingList);
        long timeA2 = System.currentTimeMillis();
        sheet.addCell(new jxl.write.Number(sorterColumn, i, benchmark.getComparisons()));
        sheet.addCell(new jxl.write.Number(sorterColumn, i + 1, benchmark.getSwaps()));
        sheet.addCell(new jxl.write.Number(sorterColumn, i + 2, timeA2 - timeA1));

        List<Integer> descendingList = generator.createDescending(n);
        long timeD1 = System.currentTimeMillis();
        sorter.sort(generator.getComparator(), descendingList);
        long timeD2 = System.currentTimeMillis();
        sheet.addCell(new jxl.write.Number(sorterColumn, i + 3, benchmark.getComparisons()));
        sheet.addCell(new jxl.write.Number(sorterColumn, i + 4, benchmark.getSwaps()));
        sheet.addCell(new jxl.write.Number(sorterColumn, i + 5, timeD2 - timeD1));*/

        List<Integer> randomList = generator.createRandom(n);
        long timeR1 = System.currentTimeMillis();
        sorter.sort(generator.getComparator(), randomList);
        long timeR2 = System.currentTimeMillis();
        sheet.addCell(new jxl.write.Number(sorterColumn, i + 6, benchmark.getComparisons()));
        sheet.addCell(new jxl.write.Number(sorterColumn, i + 7, benchmark.getSwaps()));
        sheet.addCell(new jxl.write.Number(sorterColumn, i + 8, timeR2 - timeR1));
    }

    public static void main(String[] args) {
        SorterTestExcel sorterTestExcel = new SorterTestExcel();
        sorterTestExcel.init();
    }
}
